package org.gp.core.web;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.gp.core.annotation.AuthOperate;
import org.gp.core.entity.Activity;
import org.gp.core.entity.constant.Default;
import org.gp.core.entity.dto.ResponseDTO;
import org.gp.core.entity.result.ActivityDetailResult;
import org.gp.core.service.IActivityService;
import org.gp.core.utils.HobbyConvertUtil;
import org.gp.core.web.param.SeekActParam;
import org.gp.core.web.param.SponsorActParam;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 活动信息表  前端控制器
 * </p>
 *
 * @author LuoLiang
 * @since 2017-01-24
 */
@RestController
@RequestMapping("/activity")
@Slf4j
public class ActivityController {
    @Resource
    private IActivityService activityService;

    @RequestMapping(value = "sponsoract", method = RequestMethod.POST)
    public String sponsorAct(SponsorActParam param, @CookieValue("USERID") Integer userId) throws ParseException {
        log.info("param:{},userId:{}", JSON.toJSONString(param), userId);
        Activity act = new Activity();
        act.setId(param.getId());
        act.setBudget(param.getBudget());
        act.setIntroduction(param.getIntroduction());
        act.setLabel(HobbyConvertUtil.getHobbyCode(param.getType()));
        act.setName(param.getName());
        act.setStartdate(DateUtils.parseDate(param.getStartDate(), "yyyy-MM-dd HH:mm"));
        act.setPersonNum(param.getPersonNum());
        act.setPlace(param.getPlace());
        act.setSponsorUserid(userId);

        ResponseDTO dto = activityService.sponsorActivity(act);

        return JSON.toJSONString(dto);
    }

    /**
     * 查询返回活动列表接口
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "seekact", method = RequestMethod.GET)
    public String SeekActivity(SeekActParam param, HttpServletRequest request) {
        String userId = null;
        if (param.getSort() != null) {
            Cookie[] cookies = request.getCookies();
            userId = cookies[0].getValue();
            log.debug("cookies:" + JSON.toJSONString(cookies)+" userId:"+cookies[0].getValue());
        }
        ResponseDTO dto = activityService.getActListByHobby(param, Integer.valueOf(userId));

        return JSON.toJSONStringWithDateFormat(dto, Default.ACTIVITY_TIME_TYPE);
    }

    @RequestMapping(value = "relatedActivity", method = RequestMethod.GET)
    public String relatedActivity(Integer actId, Integer sponsorId, Integer currentPage, Integer pageSize) {
        ResponseDTO dto = activityService.relatedActivity(actId, sponsorId);

        return JSON.toJSONStringWithDateFormat(dto, Default.ACTIVITY_TIME_TYPE);
    }

    /**
     * web API 查询活动列表
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "activityList", method = RequestMethod.GET)
    @AuthOperate
    public String activityList(SeekActParam param) {
        ResponseDTO dto = activityService.getActListByHobby(param, null);

        return JSON.toJSONStringWithDateFormat(dto, Default.ACTIVITY_TIME_TYPE);
    }

    /**
     * 获取单个活动详情接口
     *
     * @param actId
     * @return
     */
    @RequestMapping(value = "activitydetail", method = RequestMethod.GET)
    public String activityDetail(Integer actId) {
        ResponseDTO<ActivityDetailResult> dto = activityService.getActivityDetailInfo(actId);

        return JSON.toJSONStringWithDateFormat(dto, Default.ACTIVITY_TIME_TYPE);
    }

    /**
     * 用户加入活动接口
     *
     * @param userId
     * @param actId
     * @return
     */
    @RequestMapping(value = "joinactivity", method = RequestMethod.POST)
    public String joinActivity(@CookieValue("USERID") Integer userId, Integer actId, String introduce, String phone) {
        ResponseDTO dto = activityService.joinActivity(userId, actId, introduce, phone);

        return JSON.toJSONString(dto);
    }

    /**
     * 获取用户参加活动列表接口
     *
     * @param userId
     * @param flag
     * @return
     */
    @RequestMapping(value = "useractivitylist", method = RequestMethod.GET)
    public String userActivityList(@CookieValue("USERID") Integer userId, String flag) {
        ResponseDTO<List<ActivityDetailResult>> dto = activityService.getActListByUser(userId, flag);

        return JSON.toJSONStringWithDateFormat(dto, Default.ACTIVITY_TIME_TYPE);
    }

    @RequestMapping(value = "passactivity", method = RequestMethod.POST)
    public String passActivity(Integer passId, Integer sponsorId) {
        ResponseDTO dto = activityService.passActivity(passId, sponsorId);

        return JSON.toJSONString(dto);
    }

    @RequestMapping(value = "refuseActivity", method = RequestMethod.POST)
    public String refuseActivity(Integer passId, Integer sponsorId, String reason) {
        ResponseDTO dto = activityService.refuseActivity(passId, sponsorId, reason);

        return JSON.toJSONString(dto);
    }

    @RequestMapping(value = "getActivity", method = RequestMethod.GET)
    public String getActivity(Integer actId) {
        return JSON.toJSONStringWithDateFormat(activityService.getActivity(actId), "yyyy-MM-dd HH:mm");
    }

    @RequestMapping(value = "admininfo", method = RequestMethod.GET)
    public String adminInfo() {
        return JSON.toJSONString(activityService.initAdminInfo());
    }
}
